package com.atguigu.guliedu.banner.controller;


import com.atguigu.common.utils.R;
import com.atguigu.guliedu.banner.entity.Banner;
import com.atguigu.guliedu.banner.service.BannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author java
 * @since 2021-02-02
 */
@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    /**
     * 分页查询
     * @return
     */
    @GetMapping("list")
    public R list(@RequestParam Map<String,String> params){
        Page<Banner> list = bannerService.listBanner(params);
        return R.ok().data("data",list);
    }

    /**
     * 通过id删除
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R removeById(@PathVariable String id){
        bannerService.removeById(id);
        return R.ok();
    }

    /**
     * 更新
     * @param banner
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody Banner banner){
        banner.setGmtModified(new Date());
        System.out.print(new Date());
        bannerService.updateById(banner);
        return R.ok();
    }

    /**
     * 保存
     * @param banner
     * @return
     */
    @PutMapping("save")
    public R save(@RequestBody Banner banner){
        banner.setGmtCreate(new Date());
        banner.setGmtModified(new Date());
        bannerService.save(banner);
        return R.ok();
    }

    /**
     * 通过id获取banner
     * @param id
     * @return
     */
    @GetMapping("get/{id}")
    public R getById(@PathVariable String id){
        Banner banner = bannerService.getById(id);
        return R.ok().data("data",banner);
    }

    /**
     * 修改显示状态
     * @param banner
     * @return
     */
    @PostMapping("status")
    public R setStatus(@RequestBody Banner banner){
        bannerService.updateById(banner);
        return R.ok();
    }

    /**
     * 前端获取要显示的banner
     * @return
     */
    @GetMapping("get")
    public R getBanner(){
        List<Banner> list = bannerService.getBanner();
        return R.ok().data("data",list);
    }


}

